<?php 


//update_option('siteurl','http://localhost:8080/colisshop');update_option('home','http://localhost:8080/colisshop');
function register_my_menu() {
  register_nav_menu('header-menu',__( 'Header Menu' ));
}
if(!function_exists('t')){
function t($data,$exit=1) {
	echo '<pre style="text-align:left;">';
	print_r($data);
	echo '</pre>';
	if($exit)exit;
}
}

function replaceUnicodeChar($text="",$special="-"){
		$finds=array('á','à','ả','ã','ạ','ă','ắ','ằ','ẳ','ẵ','ặ','â','ấ','ầ','ẩ','ẫ','ậ');
		$text=str_replace($finds, 'a', $text);
		$finds=array('Á','À','Ả','Ã','Ạ','Ă','Ắ','Ằ','Ẳ','Ẵ','Ặ','Â','Ấ','Ầ','Ẩ','Ẫ','Ậ');
		$text=str_replace($finds, 'A', $text);
		$finds=array('ó','ò','ỏ','õ','ọ','ơ','ớ','ờ','ở','ỡ','ợ','ô','ố','ồ','ổ','ỗ','ộ');
		$text=str_replace($finds, 'o', $text);
		$finds=array('Ó','Ò','Ỏ','Õ','Ọ','Ơ','Ớ','Ờ','Ở','Ỡ','Ợ','Ô','Ố','Ồ','Ổ','Ỗ','Ộ');
		$text=str_replace($finds, 'O', $text);		
		$finds=array('é','è','ẻ','ẽ','ẹ','ê','ế','ề','ể','ễ','ệ');		
		$text=str_replace($finds, 'e', $text);
		$finds=array('É','È','Ẻ','Ẽ','Ẹ','Ê','Ế','Ề','Ể','Ễ','Ệ');		
		$text=str_replace($finds, 'E', $text);
		$finds=array('ú','ù','ủ','ũ','ụ','ư','ứ','ừ','ử','ữ','ự');		
		$text=str_replace($finds, 'u', $text);
		$finds=array('Ú','Ù','Ủ','Ũ','Ụ','Ư','Ứ','Ừ','Ử','Ữ','Ự');		
		$text=str_replace($finds, 'U', $text);
		$finds=array('í','ì','ỉ','ĩ','ị');		
		$text=str_replace($finds, 'i', $text);
		$finds=array('Í','Ì','Ỉ','Ĩ','Ị');		
		$text=str_replace($finds, 'I', $text);
		$finds=array('ý','ỳ','ỷ','ỹ','ỵ');		
		$text=str_replace($finds, 'y', $text);
		$finds=array('Ý','Ỳ','Ỷ','Ỹ','Ỵ');		
		$text=str_replace($finds, 'Y', $text);
		$finds=array('đ');		
		$text=str_replace($finds, 'd', $text);
		$finds=array('Đ');		
		$text=str_replace($finds, 'D', $text);		

		$text= preg_replace('/[^a-zA-Z0-9]/i', ' ', $text);
		$text= str_replace(' ',$special,trim($text));
		$text= preg_replace('/-+/i', $special, $text);
		return $text;
	
	}

// The function that handles the AJAX request
function get_data_by_request_url() {
	
  check_ajax_referer( 'my-special-stringa', 'security' );
 $requestUrl=isset($_GET['requestUrl'])?$_GET['requestUrl']:'';
 $rt=array();
 
//t($requestUrl);
 //check home page
 if(home_url('/')==$requestUrl){
 	$rt=getHomeData();
 } 
 else if(strpos($requestUrl,'/category/')!==false){
 	$cat=get_category_by_path($requestUrl,false);
 	$rt=getCatData($cat);
 }
 else {
 	
	$postid = url_to_postid($requestUrl);
	$post=get_post($postid);
	$rt=getPostData($post);
	 
 }

 //$expires = 60 * 15;        // 15 minutes
		//header('Pragma: public');
		//header('Cache-Control: maxage=' . $expires);
		//header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
	 	header('Content-type: application/json');
	 	
	echo $rt; 
	 	
  die(); // this is required to return a proper result
}
add_action( 'wp_ajax_nopriv_getpagedata', 'get_data_by_request_url' );



function getsearchdata(){
	$rt=array(); 
	check_ajax_referer( 'my-special-stringa', 'security' );
 	$searchText=isset($_GET['searchText'])?$_GET['searchText']:'';
 	//t(sanitize_text_field($searchText));
 	if(!empty($searchText)){
 		$args = array(
	 		
			'orderby' => 'id',
			'order' => 'DESC',
 			
		    
			//'category__in'=>$arrcat,
	 		//'post__in'  => get_option( 'sticky_posts' ),
			//'ignore_sticky_posts' => 1
		);

		 $lspost=query_posts($args);
		
		$posts=array();
		foreach($lspost as $k=>$v){
		 	
		 	if(strpos($v->post_name, replaceUnicodeChar($searchText))!==false){
		 		$post=array();
			 	$post['post_excerpt']=$v->post_excerpt;
			 	if(empty($v->post_excerpt)){
			 		$post['post_excerpt']=get_excerpt_by_content(500,$v->post_content);
			 	}
			 	
			 	$post['post_date_gmt']=date('d-m-Y, h:s',strtotime($v->post_date_gmt));
			 	$post['post_title']=$v->post_title;
			 	$feature_image_url=wp_get_attachment_image_src( get_post_thumbnail_id($v->ID),'medium');
			 	$post['feature_image']=$feature_image_url[0];
			 	$post['permalink'] = get_permalink($v->ID);
			 	//t($post);
			 	
			 	
			 	$posts['news'][]=$post;
			 	
		 	}
		 }
		 
		 $rt['menutab']='menu'.$cat->slug;
			$rt['title']='Tìm kiếm từ khóa '.$searchText;
		 $rt['posts']=$posts;
		 
	}
	echo json_encode($rt);
	exit;	
}
add_action( 'wp_ajax_nopriv_getsearchdata', 'getsearchdata' );


function getCatData($cat=null){
	$rt=array(); 
	if($cat!=null){
		$args = array(		 
			'limit'=>5,		
				'post_status'=>'publish',
			'orderby' => 'id',
			'order' => 'DESC',
			'category__in'=>array($cat->cat_ID)
			);						
		 $lspost=query_posts($args);
		 //t($lspost);
		 $posts=['news','others'];		 
		 foreach($lspost as $k=>$v){
		 	$post=array();
		 	$post['post_excerpt']=$v->post_excerpt;
		 	if(empty($v->post_excerpt)){
		 		$post['post_excerpt']=get_excerpt_by_content(500,$v->post_content);
		 	}
		 	
		 	$post['post_date_gmt']=date('d-m-Y, h:s',strtotime($v->post_date_gmt));
		 	$post['post_title']=$v->post_title;
		 	$feature_image_url=wp_get_attachment_image_src( get_post_thumbnail_id($v->ID),'medium');
		 	$post['feature_image']=$feature_image_url[0];
		 	$post['permalink'] = get_permalink($v->ID);
		 	//t($post);
		 	
		 	if($k<2){
		 		$posts['news'][]=$post;
		 	}
		 	else{
		 		$posts['others'][]=$post;
		 	}
		 }
		 
		 $rt['menutab']='menu'.$cat->slug;
		 $rt['model']='Category';
		 $rt['title']=$cat->cat_name;
		 $rt['posts']=$posts;
		 
	}
	 //t($rt);
	 return json_encode($rt);	 
}
function getPostData($post){
	$rt=array(); 
	$cat=get_the_category($post->ID);
	$menutab='menu';
	
	if(!empty($cat)){
		$menutab	.=$cat[0]->slug;
	}
	else{
		$menutab.=$post->post_name;
	}
	
	 
	 $rt['menutab']=$menutab;
	 $rt['title']=$post->post_title;	 
	 $rt['content']=$post->post_content;
	 //t($rt);
	 return json_encode($rt); 
}
function getHomeData(){
	$rt=array();
	$arrcat=array();
  	//$cat=get_category_by_slug('dich-vu');
  	//$arrcat[]=$cat->cat_ID;
  	//$cat=get_category_by_slug('day-xam-2');
	 //$arrcat[]=$cat->cat_ID;
	 //$cat=get_category_by_slug('cham-soc-da');
	 //$arrcat[]=$cat->cat_ID;
	 $args = array(
	 		'showposts'=>4,
			'orderby' => 'id',
			'order' => 'DESC',
			//'category__in'=>$arrcat,
	 		//'post__in'  => get_option( 'sticky_posts' ),
			//'ignore_sticky_posts' => 1
		);						
	 $lspost=query_posts($args);
	 $posts=array();
	 foreach($lspost as $k=>$v){
	 	$post=array();
	 	$post['post_excerpt']=$v->post_excerpt;
	 	if(empty($v->post_excerpt)){
	 		$post['post_excerpt']=get_excerpt_by_content(200,$v->post_content);
	 	}
	 	$post['post_title']=$v->post_title;
	 	$feature_image_url=wp_get_attachment_image_src( get_post_thumbnail_id($v->ID),'medium');
	 	$post['feature_image']=$feature_image_url[0];
	 	$post['permalink'] = get_permalink($v->ID);
	 	//t($post);
	 	$posts[]=$post;
	 }
	 //$rt['model']='Home';
	 $rt['menutab']='menuhome';
	 $rt['title']=get_bloginfo( 'name' );
	 $rt['posts']=$posts;
	 //t($rt);
	 return json_encode($rt); 
}
function getMenuData(){
	$rt=array();
  	$cat=get_category_by_slug('dich-vu');
	 $args = array(
	 		'showposts'=>4,
			'orderby' => 'id',
			'order' => 'DESC',
			'category__in'=>array($cat->cat_ID),
			 'post__in'  => get_option( 'sticky_posts' ),
			'ignore_sticky_posts' => 1
		);						
	 $lspost=query_posts($args);
	 $posts=array();
	 foreach($lspost as $k=>$v){
	 	$post=array();	 	
	 	$post['post_title']=$v->post_title;	 	
	 	$post['permalink'] = get_permalink($v->ID);
	 	//t($post);
	 	$posts[]=$post;
	 }	 
	 $catdata['posts']=$posts;
	 $catdata['title']=$cat->cat_name;
	 
	 //t($catdata);
	 $rt[]=$catdata;
	 
	 
	 
	 $cat=get_category_by_slug('day-xam-2');
	 $arrcat[]=$cat->cat_ID;
	 $cat=get_category_by_slug('cham-soc-da');
	 $arrcat[]=$cat->cat_ID;
	 $args = array(
	 		'showposts'=>4,
			'orderby' => 'id',
			'order' => 'DESC',
			'category__in'=>$arrcat,
			 'post__in'  => get_option( 'sticky_posts' ),
			'ignore_sticky_posts' => 1
		);						
	 $lspost=query_posts($args);
	 $posts=array();
	 foreach($lspost as $k=>$v){
	 	$post=array();	 	
	 	$post['post_title']=$v->post_title;	 	
	 	$post['permalink'] = get_permalink($v->ID);
	 	//t($post);
	 	$posts[]=$post;
	 }	 
	 $catdata['posts']=$posts;
	 $catdata['title']=$cat->cat_name;
	 $rt[]=$catdata;
	 
	 
	 
	 
	 return json_encode($rt); 
}

// Changing excerpt length
function get_excerpt_by_content($count,$content){    
    $excerpt = $content;
    $excerpt = trim(strip_tags($excerpt));
    $excerpt = substr($excerpt, 0, $count);
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = $excerpt.'...';
    return $excerpt;
}

//add_theme_support( 'post-thumbnails' );
add_theme_support('post-thumbnails', array('post'));
function is_category_in($catname,$cats){
	foreach($cats as $k=>$v)
		if($v->name==$catname)return true;
	return false;
}

//=================== enqueue style =========

function home_style(){
	global $post,$cat,$counter;	
	//$cats=(get_the_category($post));	
	//prepare data
	$data='';
	if(is_home()){
		$data=getHomeData();
	}
	else if(is_single() || is_page()){
		$data=getPostData($post);
	}
	else if(is_category()){	
		$cats=get_the_category();	
		$data=getCatData($cats[0]);
	}
	
	//menu data
	$menudata=getMenuData();
	
	wp_enqueue_style('bootstrap',get_template_directory_uri() . '/css/bootstrap.min.css');
	wp_enqueue_style('home',get_template_directory_uri() . '/style.css',array());
	wp_enqueue_style('fixbootstrap',get_template_directory_uri() . '/css/fixbootstrap.css',array());
	//script
	
	wp_enqueue_script('bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js',array('jquery'));
	wp_enqueue_script('zweatherfeed', get_template_directory_uri() . '/js/jquery.zweatherfeed.min.js',array('jquery'));
	wp_enqueue_script('angularjs', get_template_directory_uri() . '/js/angularjs/angular.min.js',array('jquery'));
	
	wp_enqueue_script('maincontroller', get_template_directory_uri() . '/js/angularjs/maincontroller.js',array());
	wp_enqueue_script('detailcontroller', get_template_directory_uri() . '/js/angularjs/detailcontroller.js',array());
	wp_enqueue_script('newscontroller', get_template_directory_uri() . '/js/angularjs/newscontroller.js',array());
	wp_enqueue_script('app', get_template_directory_uri() . '/js/angularjs/app.js');
	//wp_enqueue_script('headermenucontroller', get_template_directory_uri() . '/js/angularjs/headermenucontroller.js',array('app'));	
	wp_localize_script( 'app', 'MyAjax', array(
	    // URL to wp-admin/admin-ajax.php to process the request
	    'ajaxurl' => admin_url( 'admin-ajax.php' ), 
	    // generate a nonce with a unique ID "myajax-post-comment-nonce"
	    // so that you can check it later when an AJAX request is sent
	    'security' => wp_create_nonce( 'my-special-stringa' ),
		'app_path' => 'http://localhost:8080/vijeco',
		'predata'=> $data,
		'menudata'=> $menudata,
  	));
  	
  	
  	wp_enqueue_script('mycommon', get_template_directory_uri() . '/js/common.js');
  	
  	
  	
  	
}
add_action( 'wp_enqueue_scripts', 'home_style' );

function getcounter(){
	$counter=get_site_option('counter');
  	if(!$counter){
  		$counter=2578;
  		add_option('counter',$counter);
  	}
  	update_option('counter',$counter+1);
  	return $counter;
}
//=================== excerpt setting =========
function custom_excerpt_length( $length ) {
	return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
function new_excerpt_more( $more ) {
	return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');

//================ tinymce config ===============
function mytheme_tinymce_config( $init ) {
 $valid_iframe = 'iframe[id|class|title|style|align|frameborder|height|longdesc|marginheight|marginwidth|name|scrolling|src|width]';
 if ( isset( $init['extended_valid_elements'] ) ) {
  $init['extended_valid_elements'] .= ',' . $valid_iframe;
 } else {
  $init['extended_valid_elements'] = $valid_iframe;
 }
 return $init;
}
add_filter('tiny_mce_before_init', 'mytheme_tinymce_config');

add_action( 'init', 'register_my_menu' );
?>